﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace HomeWork_03
{
    public interface Shape //声明图形类接口
    {
        double area();
        Boolean Islegal();
        void Show();//显示图形信息
    }
    class Rectangle : Shape
    {
        private double length;
        private double width;
        public double Length
        {
            set { length = value; }
            get { return length; }
           
        }
        public double Width
        {
            set { width = value; }
            get { return width; }
        }
        

       public Rectangle(double length,double width)
        {
            this.length = length;
            this.width = width;
        }
        public double area()
        {
            return length * width;
        }
        public Boolean Islegal()
        {
            if (width > 0 && length > 0)
                return true;
            else
                return false;
        }
        public void Show()
        {
            Console.WriteLine("the length of Rectangle: " + length + " the width of Rectangle :" + width);
        }
    }
    class Square : Shape
    {
        private double side;
        public double Side
        {
            set;get;
        }
        public void Show()
        {
            Console.WriteLine("the side of Square: " + side);
        }
        public Square(double side)
        {
            this.side = side;
        }
        public double area()
        {
            return side * side;
        }
        public Boolean Islegal()
        {
            if (side > 0)
                return true;
            else
                return false;
        }
    }
    class Triangle : Shape
    {
        private double a;
        private double b;
        private double c;
        public double A
        {
            get { return a; }
            set { a = value; }
        }
        public double B
        {
            get { return b; }
            set { b = value; }
        }
        public double C
        {
            get { return c; }
            set { c = value; }

        }
        public void Show()
        {
            Console.WriteLine("the three edges of Triangle:" + a + " " + b + " " + c);
        }
        public Triangle(double a,double b,double c)
        {
            this.a = a;
            this.b = b;
            this.c = c;
        }
        public double area()
        {
            double p = (a + b + c) / 2;
            return Math.Sqrt(p * (p - a) * (p - b) * (p - c));
        }
        public Boolean Islegal()
        {
            double p = (a + b + c) / 2;
            if (p - a > 0 && p - b > 0 && p - c > 0)
                return true;
            else
                return false;
        }
    }
    class Program
    {
        public static Shape FactoryShape(string type)  //工厂方法，传入string类型参数返回对应图形类
        {
            Shape shape = null;
            Random side = new Random();
            switch (type)
            {
                case "Rectangle":
                     shape = new Rectangle(side.Next(1, 10), side.Next(1, 10));
                    break;
                case "Triangle":
                    shape = new Triangle(side.Next(5, 10), side.Next(5, 10), side.Next(5, 10));
                    break;
                case "Square":
                    shape = new Square(side.Next(1, 10));
                    break;
            }
            return shape;
        }
        public static void CreatShape(int num) //num决定生成多少个图形
        {
            Random rd = new Random();
            double sum = 0;
            
            for (int i = 0; i < num; i++)
            {
                double flag = (double)rd.Next(2, 8); //通过随机数来达到随机生成图形的目的
               
                if (flag >= 2 && flag < 4)
                {
                    
                    Shape shape = FactoryShape("Rectangle");
                    shape.Show();
                    Console.WriteLine("shape is a Rectangle,the area is:" + shape.area());
                    Console.WriteLine();
                    sum += shape.area();
                }
                
                else if (flag >= 4 && flag < 6)
                {
                    
                    Shape shape = FactoryShape("Triangle");
                    shape.Show();
                    Console.WriteLine("shape is a Triangle,the area is:" + shape.area());
                    Console.WriteLine();
                    sum += shape.area();
                }
                   
                else if (flag >= 6 && flag < 8)
                {
                    
                    Shape shape = FactoryShape("Square");
                    shape.Show();
                    Console.WriteLine("shape is a Square,the area is:" + shape.area());
                    Console.WriteLine();
                    sum+= shape.area();
                }
                    
            }
            Console.WriteLine("面积总和为：" + sum);
        }
        static void Main(string[] args)
        {
            int num = 10;
            Program.CreatShape(num);

            
        }
    }
}
